package com.qiantu.phone.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.BaseAdapter;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.BeforePagataBean;
import com.qiantu.api.entity.HikallBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.bean.TypeLimitBean;
import com.qiantu.phone.ui.dialog.BottomCurfewTimeDialog;
import com.qiantu.phone.ui.dialog.BottomtypeDialog;
import com.lxj.xpopup.XPopup;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;


/**
 * @Description: 门禁事件
 */

public class DeviceHiKallActivity extends AppActivity implements OnRefreshLoadMoreListener {
    private TextView tvType;
    private TextView tvDataTime;
    private RecyclerView rvHiKall;
    private BottomtypeDialog mBottomtypeDialog;
    private BottomCurfewTimeDialog mBottomCurfewTimeDialog;
    private String houseSerialNo;
    private int executionType = 100;    //默认全部类型
    private HiKallAdapter mHiKallAdapter;
    private HiKallAdapter2 mHiKallAdapter2;
    private List<HikallBean.AfterTimeRecords> afterTimeRecords = new ArrayList<>();
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");// HH:mm:ss
    private String datatime;
    private SmartRefreshLayout smartR;
    private int pageIndex = 1;
    private int pageSize = 10;
    //1初始化  2刷新  3加载更多
    private int requestType = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_hi_kall;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        houseSerialNo = intent.getStringExtra("houseSerialNo");
        smartR = findViewById(R.id.smartR);
        tvType = (TextView) findViewById(R.id.tv_type);
        tvDataTime = (TextView) findViewById(R.id.tv_dataTime);
        rvHiKall = (RecyclerView) findViewById(R.id.rv_hiKall);

        initAdapter();
        upDataView();
        setOnClickListener(tvType,tvDataTime);
    }

    @Override
    protected void initData() {
        initHiKall();
    }

    private void upDataView() {
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        datatime = simpleDateFormat.format(date);
        tvDataTime.setText(datatime);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_type:
                    showBottomtypeDialog();
                break;
            case R.id.tv_dataTime:
                showBottomCurfewTimeDialog(tvDataTime.getText().toString());
                break;
        }
    }


    private void initAdapter() {
        mHiKallAdapter2 = new HiKallAdapter2(this);
//        mHiKallAdapter = new HiKallAdapter();
        rvHiKall.setAdapter(mHiKallAdapter2);
        smartR.setOnRefreshLoadMoreListener(this);
        rvHiKall.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initHiKall() {
        BeforePagataBean beforePagataBean = new BeforePagataBean();
        beforePagataBean.setNum(pageSize);
        beforePagataBean.setPagNum(pageIndex);
        LLHttpManager.getHiKall(this,beforePagataBean,datatime,houseSerialNo,executionType,new HttpCallback<HttpData<HikallBean>>(this){
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<HikallBean> result) {
                super.onSucceed(result);
                if (result.getData().getBeforeTimeRecords().size()!=0){
                    refreshdata(result.getData().getBeforeTimeRecords());
                }else if (result.getData().getAfterTimeRecords().size()!=0){
                    refreshdata(result.getData().getAfterTimeRecords());
                }else {
                    if (requestType == 1){
                        mHiKallAdapter2.clearData();
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
                switch (requestType){
                    case 1:
                        if (mHiKallAdapter2.getData() == null || mHiKallAdapter2.getData().size() == 0){

                        }else {
//                            smartR.setNoMoreData(false);
                        }
                        break;
                    case 2:
                        smartR.finishRefresh();
                        break;
                    case 3:
                        smartR.finishLoadMore();
                        smartR.setNoMoreData(mHiKallAdapter2.isLastPage());
                        break;
                }
            }
        });
    }

    private void refreshdata(List<HikallBean.BeforeTimeRecords> beforeTimeRecords) {
        if (beforeTimeRecords != null) {
            switch (requestType){
                case 1:
                    mHiKallAdapter2.setData(beforeTimeRecords);
                    //列表数据更新之后，重新定义这两个值
                    mHiKallAdapter2.setLastPage(beforeTimeRecords.size() > 10);
                    smartR.setNoMoreData(mHiKallAdapter2.isLastPage());
                    break;
                case 2:
                    mHiKallAdapter2.clearData();
                    mHiKallAdapter2.setData(beforeTimeRecords);
                    break;
                case 3:
                    mHiKallAdapter2.addData(beforeTimeRecords);
                    mHiKallAdapter2.setLastPage(beforeTimeRecords.size() < 10);
                    break;
            }
        }
    }

    //上拉加载
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestType = 3;
        pageIndex = (int) Math.ceil(((float) mHiKallAdapter2.getData().size() / pageSize)) + 1;
        initHiKall();
    }

    //下拉刷新
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        requestType = 2;
        pageIndex = 1;
        initHiKall();
    }

    private class HiKallAdapter2 extends AppAdapter<HikallBean.BeforeTimeRecords>{

        public HiKallAdapter2(@NonNull Context context) {
            super(context);
        }

        @NonNull
        @Override
        public BaseAdapter<?>.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder();
        }

        private class ViewHolder extends AppAdapter<?>.ViewHolder{
            private TextView tv_time;
            private TextView tv_event;

            private ViewHolder() {
                super(R.layout.item_incident);
                tv_time = findViewById(R.id.tv_time);
                tv_event = findViewById(R.id.tv_event);
            }

            @Override
            public void onBindView(int position) {
                tv_time.setText(getData().get(position).getTime());
                tv_event.setText(getData().get(position).getTitle());
            }
        }
    }

    private class HiKallAdapter extends BaseQuickAdapter<HikallBean.BeforeTimeRecords, BaseViewHolder>{

        public HiKallAdapter(){
            super(R.layout.item_incident);
        }

        @Override
        public void setNewData(@Nullable List<HikallBean.BeforeTimeRecords> data) {
            super.setNewData(data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, HikallBean.BeforeTimeRecords item) {
            holder.setText(R.id.tv_time,item.getTime());
            holder.setText(R.id.tv_event,item.getTitle());
        }
    }

    //门禁事件类型
    private void showBottomtypeDialog(){
        if (mBottomtypeDialog == null) {
            mBottomtypeDialog = new BottomtypeDialog(this);
            mBottomtypeDialog.setDialogClickListener(new BottomtypeDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(TypeLimitBean typeLimitBean) {
                    tvType.setText(typeLimitBean.getLabel());
                    executionType = Integer.parseInt(typeLimitBean.getValue());
                    requestType = 1;
                    pageIndex = 1;
                    initHiKall();
                    mBottomtypeDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomtypeDialog);
        }
        mBottomtypeDialog.show();
    }
    //事件时间
    private void showBottomCurfewTimeDialog(String tvdatatime) {
        if (mBottomCurfewTimeDialog == null) {
            mBottomCurfewTimeDialog = new BottomCurfewTimeDialog(this);
            mBottomCurfewTimeDialog.setDialogClickListener(new BottomCurfewTimeDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(boolean isOpen, String startTime) {
                    tvDataTime.setText(startTime);
                    datatime = startTime;
                    requestType = 1;
                    initHiKall();
                    mBottomCurfewTimeDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomCurfewTimeDialog);
        }
        mBottomCurfewTimeDialog.initDatatime(tvdatatime);
        mBottomCurfewTimeDialog.show();
    }

}